package com.lagou.page.controller;

import com.lagou.common.pojo.Orders;
import com.lagou.common.pojo.OrdersVo;
import com.lagou.common.pojo.Products;

import com.lagou.page.fegin.OrdersFeign;
import com.lagou.page.fegin.ProductFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/page")
public class PageController {
    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private OrdersFeign ordersFeign;

    @GetMapping("/order/query/{id}")
    public Orders queryById(@PathVariable Integer id){
        return  ordersFeign.queryById(id);
    }

    @ResponseBody
    @GetMapping("/order/queryList")
    public OrdersVo queryList(@RequestParam(value = "current")Integer current, @RequestParam(value = "size")Integer size){
        System.out.println(current);
        System.out.println(size);
        return ordersFeign.queryList(current,size);
    }

    @GetMapping("/getProduct/{id}")
    public Products getProduct(@PathVariable Integer id){
        return productFeign.queryById(id);
    }

    @GetMapping("/loadProductServicePort")
    public String getProductServerPort(){

        return productFeign.getPort();

    }

    /*
    * 模拟服务超时熔断处理
    * 针对熔断处理，hystrix默认维护一个线程池，默认大小为10
    * */
    @HystrixCommand(
            threadPoolKey = "loadProductServicePort2",//默认所有的请求共同维护一个线程池
            //每一个属性对应的都是一个hystrixProperty
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize",value = "1"),//并发的线程数
                    @HystrixProperty(name = "maxQueueSize",value = "20"),//默认线程队列是-1，默认不开启

            },
            //超时时间的设置
            commandProperties = {
                    //每一个属性对应的都是一个hystrixProperty
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
            }

    )
    @GetMapping("/loadProductServicePort2")
    public String getProductServerPort2(){

        return productFeign.getPort();

    }
    @HystrixCommand(
            commandProperties = {
                    //每一个属性对应的都是一个hystrixProperty
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000"),
                    //统计窗口时间的设置
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "8000"),
                    //统计窗口内的最小请求数
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "2"),
                    //统计窗口内错误请求阈值的设置 50%
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),
                    // 自我修复的活动窗口时间
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "3000")
            },//设置回退方法
            fallbackMethod = "getProductServerPortFallBack"
    )
    @GetMapping("/loadProductServicePort3")
    public String getProductServerPort3(){

        return productFeign.getPort();

    }

    /*
    * 定义回退方法，当请求触发熔断后执行，补救措施
    * 注意：
    * 1.方法的形参和我们的原方法保持一致
    * 2.方法的返回值与原方法保持一致
    *
    * */
    public String getProductServerPortFallBack(){
        return "-1";
    }



}
